unit StrUtilsEx;
interface

{
 LastPos(2) function returns the index of the very last occurence of a
 substring in a string, or returns 0 if there is no occurence at all.
}
 Function LastPos (substr: widestring; var str: widestring): cardinal;
 Function LastPos2(substr,str: widestring): cardinal;

{
 CopyFromTo returns a substring from a string pointed by two indexes.
 the order of these indexes is not important, means both Index1<=ndex2
 and Index1>Index2 is acceptable by the function and will return a valid
 result.
}
 Function CopyFromTo (var str: widestring; Index1,Index2: cardinal): widestring;
 Function CopyFromTo2(str: widestring; Index1,Index2: cardinal): widestring;

{
 DeleteEx deletes a substring from a string within two indexes (the order
 does not matter. Function returns the substring that was deleted, or
 an empty string if nothing was deleted.
}
 Function DeleteEx (var str: widestring; Index1,Index2: cardinal): widestring;
 Function DeleteEx2(str: widestring; Index1,Index2: cardinal): widestring;

{
 ReplaceEx returns a string in which a portion between two indexes was replaced
 with a value of a given substring (could be an empty string). If no
 replacement was possible to make, the returned string will be the original
 one.
}
 Function ReplaceEx(substr,str: widestring; Index1,Index2: cardinal): widestring;

implementation


Function ReplaceEx(substr,str: widestring; Index1,Index2: cardinal): widestring;
  var ln: integer;
  begin
    Result:=str;
    ln:=Length(str);
    if(Index1>ln)and(Index2>ln)then exit;
    if(Index1<=Index2)
       then Result:=CopyFromTo(str,1,Index1-1)+substr+Copy(str,Index2,ln)
       else Result:=CopyFromTo(str,1,Index2-1)+substr+Copy(str,Index1,ln);
  end;

Function DeleteEx(var str: widestring; Index1,Index2: cardinal): widestring;
  var ln: integer;
  begin
    Result:='';
    if(Index1<1)or(Index2<0)then exit;
    ln:=Length(str); if(Index1>ln)and(Index2>ln)then exit;
    if(Index1<Index2)then begin
                           Result:=CopyFromTo2(str,Index1,Index2);
                           Delete(str,Index1,Index2-Index1+1);
                          end
                     else begin
                           Result:=CopyFromTo2(str,Index2,Index1);
                           Delete(str,Index2,Index1-Index2+1);
                          end;
  end;

Function DeleteEx2(str: widestring; Index1,Index2: cardinal): widestring;
  begin
    Result:=DeleteEx(str,Index1,Index2);
  end;

Function CopyFromTo(var str: widestring; Index1,Index2: cardinal): widestring;
  begin
   Result:=''; if(str='')then exit;
   if(Index1<=Index2)then Result:=Copy(str,Index1,Index2-Index1+1)
                     else Result:=Copy(str,Index2,Index1-Index2+1);
  end;

Function CopyFromTo2(str: widestring; Index1,Index2: cardinal): widestring;
  begin
   Result:=CopyFromTo(str,Index1,Index2);
  end;

Function LastPos (substr: widestring; var str: widestring): cardinal;
  var ln,l,i: integer;
  begin
   Result:=0;
   ln:=Length(str); if(ln=0)then exit;
   l:=Length(substr); if(l=0)then exit;
   if(l>ln)then exit;
   For i:=ln-l+1 downto 1 do
       if Copy(str,i,l)=substr then begin Result:=i; exit; end;
  end;

Function LastPos2(substr,str: widestring): cardinal;
  begin
   Result:=LastPos(substr,str);
  end;


Begin
End.
